package com.hfzy.ihk.web.calldata.handlers;

import com.alibaba.fastjson.JSON;
import com.hfzy.ihk.common.constants.GlobalStatic;
import com.hfzy.ihk.common.core.elasticsearch.ElasticsearchOperation;
import com.hfzy.ihk.common.core.elasticsearch.OperationErrorMsgBiz;
import com.hfzy.ihk.common.core.elasticsearch.base.CommonTableOperationHandler;
import com.hfzy.ihk.common.util.CommonUtils;
import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.common.util.uuid.base64uuid.UUIDs;
import com.hfzy.ihk.common.web.WebResult;
import com.hfzy.ihk.facade.calldata.entity.CallHistoryText;
import com.hfzy.ihk.facade.calldata.enums.CallDataOperations;
import com.hfzy.ihk.facade.calldata.enums.CallDataTables;
import com.hfzy.ihk.web.calldata.biz.CallHistoryTextBiz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/24.
 */
@Component
public class CallHistoryTextTableOperationHandler extends CommonTableOperationHandler {

    private static final Logger logger = LoggerFactory.getLogger(CallHistoryTextTableOperationHandler.class);

    @Autowired
    private OperationErrorMsgBiz operationErrorMsgBiz;

    @Autowired
    CallHistoryTextBiz callHistoryTextBiz;

    public CallHistoryTextTableOperationHandler() {

        /**
         * 必须定义该处理器对应的table和操作名
         * */
        this.table = CallDataTables.CALL_HISTORY_TEXT;

        this.tableOperation = CallDataOperations.addCallHistoryText;
    }

    /**
     * 重试错误信息
     * @param elasticsearchOperation
     */
    @Override
    protected void toHandle(ElasticsearchOperation elasticsearchOperation) {

        List data = elasticsearchOperation.getData();

        logger.debug("handle data:{}", JSON.toJSONString(data));

        if(StringTools.isNotempty(data)){
            String jsonStr = data.get(0).toString();
            Map<String,Object> paramMap = JSON.toJavaObject(JSON.parseObject(jsonStr),Map.class);

            CallHistoryText callHistoryText = new CallHistoryText();
            for(String key : paramMap.keySet()){
                try {
                    CommonUtils.setObjName(callHistoryText,key,paramMap.get(key));
                } catch (Exception e) {
                    continue;
                }
            }

            if(StringTools.isEmpty(callHistoryText.getId())){
                callHistoryText.setId(UUIDs.base64UUID());
            }
            if(StringTools.isEmpty(callHistoryText.getCreateTime())){
                callHistoryText.setCreateTime(new Date());
            }
            if(callHistoryText.getVersion()==0){
                callHistoryText.setVersion(1);
            }
            WebResult result = callHistoryTextBiz.saveCallHistoryText(callHistoryText);
            if(GlobalStatic.CODE_N.equals(result.getCode())){
                throw new RuntimeException();
            }
        }

    }

    /***
     *
     * 表操作失败的处理
     * */
    public void handleError(ElasticsearchOperation elasticsearchOperation, Exception e) {

        if(elasticsearchOperation.isFromJobRun()){
            return;
        }

        logger.error("处理操作错误：",e);

        operationErrorMsgBiz.dealElasticsearchOperation(elasticsearchOperation,e);
    }
}
